/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Cliente.java
 *
 * Created on 22/04/2011, 01:17:44
 */

package tp1.views;

import tp1.controllers.Controller;
import java.sql.SQLException;
import javax.swing.JFormattedTextField;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
import tp1.dao.ClienteDao;

/**
 *
 * @author tulio
 */
public class ClientePanel extends ViewAbstract {

    /** Creates new form Cliente */
    public ClientePanel() {
        initComponents();
        initGrid();
        initToolbar();
    }

    private void initToolbar(){
        Controller controllerCliente = Controller.factory(Controller.CONTROLLER_CLIENTE, this);

        controllerCliente.addEvent(buttonSalvar, Controller.EVENTO_SALVAR_CLIENTE);
        controllerCliente.addEvent(buttonNovo, Controller.EVENTO_NOVO_CLIENTE);
        controllerCliente.addEvent(buttonEditar, Controller.EVENTO_EDITAR_CLIENTE);
        controllerCliente.addEvent(buttonApagar, Controller.EVENTO_APAGAR_CLIENTE);
        controllerCliente.addEvent(buttonBuscar, Controller.EVENTO_BUSCAR_CLIENTE);
        controllerCliente.addEvent(buttonRelatorio, Controller.EVENTO_RELATORIO_CLIENTE);
        
        buttonSalvar.addActionListener(controllerCliente);
        buttonNovo.addActionListener(controllerCliente);
        buttonApagar.addActionListener(controllerCliente);
        buttonEditar.addActionListener(controllerCliente);
        buttonBuscar.addActionListener(controllerCliente);
        buttonRelatorio.addActionListener(controllerCliente);
    }

    public void updateGridData() throws SQLException{
        Object[][] data = ClienteDao.listAsVector();
        this.setGridData(data);
    }

    public void setGridData(Object[][] data) {
        DefaultTableModel model = (DefaultTableModel) gridCliente.getModel();
        model.setDataVector(data, new String [] {
            "CPF", "Nome", "Endereço"
        });
    }

    public void reloadDefaults(){
        this.getFieldCpf().setEditable(false);
    }

    private void initGrid(){
        try {
            /**
             * Define os dados do grid
             */
            updateGridData();

            /**
             * Listener para a seleção de linhas no grid - captura valores e define-os no formulário
             */
            gridCliente.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
                public void valueChanged(ListSelectionEvent e) {
                    if(gridCliente.getModel().getRowCount() > gridCliente.getSelectedRow() && gridCliente.getSelectedRow() != -1){
                        Object cpf = gridCliente.getModel().getValueAt(gridCliente.getSelectedRow(), 0);
                        Object nome = gridCliente.getModel().getValueAt(gridCliente.getSelectedRow(), 1);
                        Object endereco = gridCliente.getModel().getValueAt(gridCliente.getSelectedRow(), 2);
                        fieldCpf.setEditable(false);
                        fieldCpf.setText(cpf.toString());
                        fieldNome.setText(nome.toString());
                        fieldEndereco.setText(endereco.toString());
                    }
                }
            });
        } catch (SQLException ex) {
            System.out.println(ex.getMessage());
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        tabs = new javax.swing.JTabbedPane();
        tabGrid = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        gridCliente = new javax.swing.JTable();
        jToolBar1 = new javax.swing.JToolBar();
        buttonNovo = new javax.swing.JButton();
        buttonEditar = new javax.swing.JButton();
        buttonApagar = new javax.swing.JButton();
        buttonBuscar = new javax.swing.JButton();
        buttonRelatorio = new javax.swing.JButton();
        tabForm = new javax.swing.JPanel();
        fieldCpf = new javax.swing.JFormattedTextField();
        labelCpf = new javax.swing.JLabel();
        labelNome = new javax.swing.JLabel();
        labelEndereco = new javax.swing.JLabel();
        buttonSalvar = new javax.swing.JButton();
        fieldNome = new javax.swing.JTextField();
        fieldEndereco = new javax.swing.JTextField();

        setPreferredSize(new java.awt.Dimension(971, 447));

        gridCliente.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "CPF", "Nome", "Endereço"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        jScrollPane1.setViewportView(gridCliente);

        jToolBar1.setRollover(true);

        buttonNovo.setText("Novo");
        buttonNovo.setFocusable(false);
        buttonNovo.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        buttonNovo.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(buttonNovo);

        buttonEditar.setText("Editar");
        buttonEditar.setFocusable(false);
        buttonEditar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        buttonEditar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(buttonEditar);

        buttonApagar.setText("Apagar");
        buttonApagar.setFocusable(false);
        buttonApagar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        buttonApagar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(buttonApagar);

        buttonBuscar.setText("Buscar");
        buttonBuscar.setFocusable(false);
        buttonBuscar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        buttonBuscar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(buttonBuscar);

        buttonRelatorio.setText("Relatório de linhas telefônias");
        buttonRelatorio.setFocusable(false);
        buttonRelatorio.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        buttonRelatorio.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(buttonRelatorio);

        javax.swing.GroupLayout tabGridLayout = new javax.swing.GroupLayout(tabGrid);
        tabGrid.setLayout(tabGridLayout);
        tabGridLayout.setHorizontalGroup(
            tabGridLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, tabGridLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(tabGridLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 662, Short.MAX_VALUE)
                    .addComponent(jToolBar1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 662, Short.MAX_VALUE))
                .addContainerGap())
        );
        tabGridLayout.setVerticalGroup(
            tabGridLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(tabGridLayout.createSequentialGroup()
                .addGap(33, 33, 33)
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 250, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        tabs.addTab("Dados", tabGrid);

        try {
            fieldCpf.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("###.###.###-##")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }

        labelCpf.setText("CPF");

        labelNome.setText("Nome");

        labelEndereco.setText("Endereço");

        buttonSalvar.setText("Salvar");

        javax.swing.GroupLayout tabFormLayout = new javax.swing.GroupLayout(tabForm);
        tabForm.setLayout(tabFormLayout);
        tabFormLayout.setHorizontalGroup(
            tabFormLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(tabFormLayout.createSequentialGroup()
                .addGap(36, 36, 36)
                .addGroup(tabFormLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(labelEndereco)
                    .addComponent(labelNome)
                    .addComponent(labelCpf))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(tabFormLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(tabFormLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(buttonSalvar)
                        .addComponent(fieldCpf, javax.swing.GroupLayout.DEFAULT_SIZE, 205, Short.MAX_VALUE)
                        .addComponent(fieldNome))
                    .addComponent(fieldEndereco, javax.swing.GroupLayout.PREFERRED_SIZE, 419, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(151, Short.MAX_VALUE))
        );
        tabFormLayout.setVerticalGroup(
            tabFormLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(tabFormLayout.createSequentialGroup()
                .addGap(103, 103, 103)
                .addGroup(tabFormLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(fieldCpf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelCpf))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(tabFormLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelNome)
                    .addComponent(fieldNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(tabFormLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(labelEndereco)
                    .addComponent(fieldEndereco, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(buttonSalvar)
                .addContainerGap(84, Short.MAX_VALUE))
        );

        tabs.addTab("Formulário", tabForm);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(35, 35, 35)
                .addComponent(tabs, javax.swing.GroupLayout.PREFERRED_SIZE, 694, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(242, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(35, 35, 35)
                .addComponent(tabs, javax.swing.GroupLayout.PREFERRED_SIZE, 369, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(43, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonApagar;
    private javax.swing.JButton buttonBuscar;
    private javax.swing.JButton buttonEditar;
    private javax.swing.JButton buttonNovo;
    private javax.swing.JButton buttonRelatorio;
    private javax.swing.JButton buttonSalvar;
    private javax.swing.JFormattedTextField fieldCpf;
    private javax.swing.JTextField fieldEndereco;
    private javax.swing.JTextField fieldNome;
    private javax.swing.JTable gridCliente;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JLabel labelCpf;
    private javax.swing.JLabel labelEndereco;
    private javax.swing.JLabel labelNome;
    private javax.swing.JPanel tabForm;
    private javax.swing.JPanel tabGrid;
    private javax.swing.JTabbedPane tabs;
    // End of variables declaration//GEN-END:variables

    public JFormattedTextField getFieldCpf() {
        return fieldCpf;
    }

    public void setFieldCpf(JFormattedTextField fieldCpf) {
        this.fieldCpf = fieldCpf;
    }

    public JTextField getFieldEndereco() {
        return fieldEndereco;
    }

    public void setFieldEndereco(JTextField fieldEndereco) {
        this.fieldEndereco = fieldEndereco;
    }

    public JTextField getFieldNome() {
        return fieldNome;
    }

    public void setFieldNome(JTextField fieldNome) {
        this.fieldNome = fieldNome;
    }

    public JTable getGridCliente() {
        return gridCliente;
    }

    public void setGridCliente(JTable gridCliente) {
        this.gridCliente = gridCliente;
    }

    public JPanel getTabForm() {
        return tabForm;
    }

    public void setTabForm(JPanel tabForm) {
        this.tabForm = tabForm;
    }

    public JPanel getTabGrid() {
        return tabGrid;
    }

    public void setTabGrid(JPanel tabGrid) {
        this.tabGrid = tabGrid;
    }

    public JTabbedPane getTabs() {
        return tabs;
    }

    public void setTabs(JTabbedPane tabs) {
        this.tabs = tabs;
    }
    
}
